package com.pureut.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.SystemDocumentFeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.api.domain.SysUser;
import com.pureut.system.api.model.LoginUser;
import com.pureut.system.domain.SysDocAudit;
import com.pureut.system.domain.SysDocCheck;
import com.pureut.system.domain.SysDocument;
import com.pureut.system.domain.vo.SysDocumentVo;
import com.pureut.system.mapper.SysDocumentMapper;
import com.pureut.system.service.ISysDocumentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 单据配置
 * @Param 实现类
 * @return
 * @Date 22-11-22
 * @Author hl
 **/
@Service
public class SysDocumentServiceImpl implements ISysDocumentService {

    @Resource
    private SysDocumentMapper sysDocumentMapper;

    @Resource
    private SystemDocumentFeignService systemDocumentFeignService;

    @Resource
    private SysMidSplitServiceImpl sysMidSplitServiceImpl;
    
    /**
     * 查询单据配置
     *
     * @param sysDocument 单据配置
     * @return 单据配置
     */
    @Override
    public List<SysDocument> selectDocumentList(SysDocumentVo sysDocument) {

        // 获取交接方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("sys_handover_method");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SysDocument> list = sysDocumentMapper.selectDocumentList(sysDocument);

        for (SysDocument ch : list){
            long docId = ch.getDocId();
            // 获取交接方式的数据字典
            ch.setDocHandoverMethodDict(netMap.get(String.valueOf(ch.getDocHandoverMethod())));
            // 获取审核配置明细list
            ch.setDocumentAuditDtoList(sysDocumentMapper.selectDocumentAuditDtoList(docId));
            // 获取检验配置明细list
            ch.setDocumentCheckDtoList(sysDocumentMapper.selectDocumentCheckDtoList(docId));
        }
        return list;

    }

    /**
     * 根据id查询详情
     * @param docId
     * @return
     */
    @Override
    public SysDocument selectDocumentListById(long docId) {
        SysDocument sys = sysDocumentMapper.selectDocumentListById(docId);
        if(sys.getDocHandoverMethod().equals("0")){
            sys.setDocHandoverMethod("");
        }
            // 获取审核配置明细list
        sys.setDocumentAuditDtoList(sysDocumentMapper.selectDocumentAuditDtoList(docId));
            // 获取检验配置明细list
        sys.setDocumentCheckDtoList(sysDocumentMapper.selectDocumentCheckDtoList(docId));
        return sys;
    }

    /**
     * 删除单据配置
     *
     * @param docId 单据配置
     * @return 单据配置
     */
    @Override
    public int deleteDocumentById(String docId) {
        String[] docIdS = docId.split(",");
        return sysDocumentMapper.deleteDocumentById(docIdS);
    }

    /**
     * 新增单据配置
     *
     * @param sysDocumentVo 单据配置
     * @return 单据配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDocument(SysDocumentVo sysDocumentVo) throws Exception{

        // 设置创建人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        sysDocumentVo.setCreateBy(sysUser.getUserName());
        sysDocumentVo.setCreateTime(new Date());
        int cn = sysDocumentMapper.insertDocument(sysDocumentVo);
        if(cn==0){
            throw new SQLException("抛出异常");
        }

        // 判断配置单据是否唯一
        int i = sysDocumentMapper.selectDocConfigByIds((long) sysDocumentVo.getDocConfig());
        if (i > 1) {
            throw new GlobalException("配置单据已存在！");
        }

        JSONObject json = sysDocumentVo.getDocConfigCode();
        JSONArray arr = json.getJSONArray("check");
       // JSONArray arr2 = json.getJSONArray("audit");
       // JSONArray arr3 = json.getJSONArray("pda");
        List<SysDocCheck> list= JSONArray.parseArray(arr.toJSONString(), SysDocCheck.class);
       // List<SysDocAudit> list2= JSONArray.parseArray(arr2.toJSONString(), SysDocAudit.class);
        long docId = sysDocumentVo.getDocId();
        for (SysDocCheck ch:
        list) {
            ch.setDocId(docId);
        }
        // 新增检验配置信息
        if(list.size()>0){
            cn = sysDocumentMapper.insertDocumentCheck(list);
        }

    /*    for (SysDocAudit au:
        list2) {
            au.setDocId(docId);
        }*/
        // 新增审核配置信息
        //cn = sysDocumentMapper.insertDocumentAudit(list2);
        return cn;
    }

    /**
     * 修改单据配置
     * @param sysDocumentVo 单据配置
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDocument(SysDocumentVo sysDocumentVo) throws SQLException {

        // 设置修改人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        sysDocumentVo.setUpdateBy(sysUser.getUserName());
        sysDocumentVo.setUpdateTime(new Date());
        // 修改单据配置信息
        int cn = sysDocumentMapper.updateDocument(sysDocumentVo);
        if(cn==0){
            throw new SQLException("抛出异常");
        }
        // 判断配置单据是否唯一
        int i = sysDocumentMapper.selectDocConfigByIds((long) sysDocumentVo.getDocConfig());
        if (i > 1) {
            throw new GlobalException("配置单据已存在！");
        }

        JSONObject json = sysDocumentVo.getDocConfigCode();
        JSONArray arr = json.getJSONArray("check");
        JSONArray arr1 = json.getJSONArray("audit");
        JSONArray arr2 = json.getJSONArray("pda");
        List<SysDocCheck> list = JSONArray.parseArray(arr.toJSONString(), SysDocCheck.class);
        List<SysDocAudit> list2 = JSONArray.parseArray(arr1.toJSONString(), SysDocAudit.class);
        long docId = sysDocumentVo.getDocId();
        // 删除检验配置信息
        sysDocumentMapper.deleteDocumentCheck(docId);
        // 删除检验审核信息
        sysDocumentMapper.deleteDocumentAudit(docId);

        for (SysDocCheck ch : list){
            ch.setDocId(docId);
        }
        if(list.size()>0){
            cn = sysDocumentMapper.insertDocumentCheck(list);
        }

        if(list2.size()>0) {
            for (SysDocAudit au : list2) {
                au.setDocId(docId);
            }
            cn = sysDocumentMapper.insertDocumentAudit(list2);
        }
        return cn;
    }

    /**
     * 根据配置单据查询单据配置详情信息
     * @param docConfig
     * @return feign远程调用接口
     */
    @Override
    public SysDocument selectDocumentConfigById(Long docConfig) {
        SysDocument sysDocument = sysDocumentMapper.selectDocumentConfigById(docConfig);
        if(sysDocument==null){
            throw new GlobalException("当前菜单未配置单据配置,请添加单据配置");
        }

            // 获取审核配置明细list
        sysDocument.setDocumentAuditDtoList(sysDocumentMapper.selectDocumentAuditDtoList(sysDocument.getDocId()));
            // 获取检验配置明细list
        sysDocument.setDocumentCheckDtoList(sysDocumentMapper.selectDocumentCheckDtoList(sysDocument.getDocId()));

        return sysDocument;
    }


    /**
     * 根据权限码返回生成编码
     * 远程调用接口
     */
    @Override
    public String selectAuthorityCoding(String docConfig) throws Exception {
        String midCode = sysDocumentMapper.selectAuthorityCoding(docConfig);
        return sysMidSplitServiceImpl.splitStr(midCode);
    }

}
